require.config({
    baseUrl:"./modules",
    paths:{
        jq:"../libs/jquery",
        gp:"getPro",
       
        cd:"changeData"

    }
})
require(["jq","gp","cd"],(_,getPro,changeData)=>{
    getPro("http://localhost:3000/database/commodity.json").then(res=>{
        // render($(".list-a"),res);
      
        $(".list-a").on("click",".shopa",function(){
            const id = $(this).parent().parent().attr("index")
            const goods = localStorage.getItem("goods") ? JSON.parse(localStorage.getItem("goods")) : [];
      
           
               const price = $(this).parent().prev().find("span").html()-0;
            
               console.log(price)
            const data = changeData(goods,id,price);
           
            localStorage.setItem("goods",JSON.stringify(data))
        })


    },code=>{
        console.log(code);
    })
     // 首页的登录守卫
     if(localStorage.getItem("token") === "ok"){
        $("#nav-a").hide()
        
        $(".shouwei").show().children("span").html(localStorage.getItem("un"))
    }
    
    // 退出功能
    $(".exit").click(function(){
        localStorage.setItem("token","");
        $("#nav-a").show()
        $(".shouwei").hide()
    })
    let goCart = document.querySelector(".shouwei-a");
    // 去购物车的登录守卫
    goCart.onclick = function(){
        if(localStorage.getItem("token") !== "ok"){
            if(confirm("你没登录，你得先登录，登不登？")){
                location.href = "./loginba.html"
            }
            return false;
        }
    }
    
    let send = document.querySelectorAll(".shopa");
    console.log(send)
    for(let i=0;i<send.length;i++){
        send[i].addEventListener("click",()=>{
            if(localStorage.getItem("token") !== "ok"){
                if(confirm("你没登录，无法加入购物车，请先登录")){
                    location.href = "./loginba.html"
                }
                return false;
            }
        })
    }
  

})

